package com.example.module_car.ui.addcar

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_car.model.AddCarEntity
import com.example.module_car.model.AddCarParam
import com.example.module_car.model.CarEntityItem
import com.example.module_car.model.ProvincesEntity
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class AddCarViewModel @Inject constructor(private val repo : AddCarRepository) : BaseViewModel() {
    private val mAddCarState_ = MutableStateFlow<RequestEvent>(Loading)
    val mAddCarState = mAddCarState_.asStateFlow()

    private val mUpDateCar_ = MutableStateFlow<RequestEvent>(Loading)
    val mUpDateCar = mUpDateCar_.asStateFlow()

     val mProvincesList = MutableLiveData<MutableList<ProvincesEntity>>()

    val mAddCar = MutableLiveData<AddCarEntity>()

    fun getProvincesList(){
        execute(
            action = repo.getProvincesList(),
            onFinish = {
                mProvincesList.value = it.data
            }
        )
    }

    fun getAddCar(param: AddCarParam){
        execute(
            action = repo.getAddCar(param),
            onError = {
                mAddCarState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mAddCarState_.value = Success
                mAddCar.value = it.data
            }
        )
    }
    fun upDateCar(id : Int , param: AddCarParam){
        execute(
            action = repo.upDateCar(id,param),
            onError = {
                mUpDateCar_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mUpDateCar_.value = Success
            }
        )
    }

}